﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Midgard
{
    enum EnemyType
    {
        Ship,
        Wizard,
        LittleShip,
        THOR
    }

    enum EnemyState
    { 
        Active,
        Dead,
        Inactive
    }

    class Enemy
    {
        const int SHIP_SCORE = 50;
        const int LITTLE_SCORE = 25;
        const int WIZZARD_SCORE = 150;
        int THOR_SCORE = 500;
        public const int MAX_HEALTH = 100;
        public const int THOR_HEALTH_MOD = 5;
        public const int BUFFER_PADDING = 10;
        int health;
        Vector2 position, projectileVelocity;
        Texture2D enemySprite, projectileSprite;
        EnemyState state;
        float speed;
        int projectileRate, projectileDamage;
        bool leftDirection;
        List<Projectile> projectiles; // Array list of projectiles
        Rectangle viewportRect;
        float timeSinceLastShot;
        float minShotTime = 1;
        float maxShotTime = 3;
        Random rnd;
        bool readyToShoot;
        EnemyType type;
        int scoreValue;
        bool HardMode = false;

        public bool ReadyToShoot
        {
            get { return readyToShoot;}
            set { readyToShoot = value; }
        }

        public EnemyState State
        {
            get { return state; }
            set { state = value; }
        }

        /* Temp Variables */
        //int currentAttackCount;

        /* Used for animation purposes
        int height = 87;
        int frame, delay, currDelay;
        Rectangle destRect, sourceRect, destRect2;
        bool reverse;
        */
        public List<Projectile> Projectiles
        {
            get { return projectiles; }
        }

        public Rectangle Bounds
        {
            get { return new Rectangle((int)position.X, (int)position.Y, enemySprite.Width, enemySprite.Height); }
        }

        public int Health
        {
            get{return health;}
        }

        public Vector2 Position
        {
            get { return position; }
            
        }

        public float Speed
        {
            get { return speed; }
        }

        public EnemyType Type
        {
            get { return type; }
        }

        public bool IsLeft
        {
            get { return this.leftDirection; }
        }

        public int Score
        {
            get { return scoreValue; }
        }


        /* Enemy no animation */
        public Enemy(int EnemyHealth, Vector2 StartPosition, Texture2D EnemySpriteTexture, bool IsLeftDirection, 
            float EnemySpeed, Rectangle ViewportRectangle, EnemyType type, bool mode)
        {
            health = EnemyHealth;
            if (type == EnemyType.THOR)
                health *= THOR_HEALTH_MOD;
            position = StartPosition;
            enemySprite = EnemySpriteTexture;
            leftDirection = IsLeftDirection;
            speed = EnemySpeed;
            viewportRect = ViewportRectangle;
            timeSinceLastShot = 0.0f;
            rnd = new Random();
            this.type = type;
            this.HardMode = mode;

            setScore(type);
            //projectileSprite = ProjectileSpriteTexture;
            //projectileVelocity = ProjectileVelocity;
            //projectileRate = ProjectileRate;
            //projectileDamage = ProjectileDamage;

            /* Initializing things */
            Initialize();


            /* Used for animation 
            frame = 0;
            destRect = new Rectangle((int)position.X, (int)position.Y, sprite.Width, height);
            sourceRect = new Rectangle(0, 0, sprite.Width, height);
            destRect2 = new Rectangle((int)position.X - Width, (int)position.Y, Width, height);
            delay = 6;
            reverse = false;
            */
        }

        public void Initialize()
        {
            state = EnemyState.Active;
            projectiles = new List<Projectile>();
        }

        public void update(GameTime gameTime)
        {
            // Checks the health
            if (health <= 0)
                state = EnemyState.Dead;

            if (HardMode)
            {
                minShotTime = (float)0.5;
                maxShotTime = 2;
            }
            else
            {
                minShotTime = 1;
                maxShotTime = 3;
            }

            /* Delete enemies if they go out of the boundary */
            if ((position.X < (0 - BUFFER_PADDING - enemySprite.Width) && leftDirection) || (position.X >= viewportRect.Width + BUFFER_PADDING && !leftDirection))
            {
                if (type == EnemyType.THOR)
                {
                    leftDirection = !leftDirection;
                    int newY = rnd.Next(EnemyManager.PLAYFIELD_UPPER_BOUND - 50, EnemyManager.PLAYFIELD_LOWER_BOUND - 50);
                    position.Y = newY;
                }
                else
                    state = EnemyState.Inactive;
            }


            /* Constant Motion */
            if (leftDirection)
            {
                //Console.WriteLine("Moving Left");
                position.X -= speed;
            }
            else
            {

                //Console.WriteLine("Moveing Right");
                position.X += speed;
            }
            timeSinceLastShot += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (timeSinceLastShot > minShotTime)
            {
                
                if (rnd.Next(2) == 1 || timeSinceLastShot > maxShotTime)
                {
                    readyToShoot = true;
                    timeSinceLastShot = 0.0f;
                }
            }


            /* Attack rate */
            /* If the current attack count is larger than the projectile rate, then the enemy can attack */
            //if (currentAttackCount > projectileRate)
            //{
            //    projectiles.Add(new Projectile(projectileSprite, new Vector2(enemySprite.Width / 2, position.Y),
            //        projectileVelocity, viewportRect, projectileDamage, 0f, Projectile.Faction.enemy));
            //}


            /* Update the Projectiles */
            //for (int i = projectiles.Count - 1; i >= 0; i--)
            //{
            //    projectiles[i].Update();

            //    if (projectiles[i].active == false)
            //    {
            //        projectiles.RemoveAt(i);
            //    }
            //}


            /* Should update the source and destination rectangles as well 
            destRect = new Rectangle((int)position.X, (int)position.Y, Width, height);
            sourceRect = new Rectangle(0, height * frame, sprite.Width, height);
            destRect2 = new Rectangle((int)position.X - Width, (int)position.Y, Width, height);
            currDelay++;

            if (currDelay > delay)
            {
                if (reverse)
                {
                    frame = (frame - 1);
                    if (frame == 0)
                        reverse = false;
                }
                else
                {
                    frame = (frame + 1) % 4;
                    if (frame == 3)
                        reverse = true;
                }



                currDelay = 0;
            }
            */
        }


        public void draw(SpriteBatch spriteBatch)
        {
            if (position.X >= -enemySprite.Width && position.X <= viewportRect.Width)
            {
                if (leftDirection)
                    spriteBatch.Draw(enemySprite, position, Color.White);
                else
                    spriteBatch.Draw(enemySprite,position,null,Color.White,0f,new Vector2(),1,SpriteEffects.FlipHorizontally,0);
            }

            //spriteBatch.Draw(enemySprite, destRect, sourceRect, Color.White);

            //for (int i = 0; i < projectiles.Count; i++)
            //{
            //    projectiles[i].Draw(spriteBatch);
            //}
        }

        public void reset(Vector2 newPostion, bool isLeft, float speed, Texture2D sprite, EnemyType type, bool mode)
        {
            position = newPostion;
            leftDirection = isLeft;
            this.speed = speed;
            health = MAX_HEALTH;
            if (type == EnemyType.THOR)
                health *= THOR_HEALTH_MOD;
            this.enemySprite = sprite;
            this.type = type;
            state = EnemyState.Active;
            this.HardMode = mode;
        }

        public void hit(int damage)
        {
            health = health - damage;
        }

        private void setScore(EnemyType type)
        {

            switch (type)
            {
                case EnemyType.Ship:
                    scoreValue = SHIP_SCORE;
                    break;
                case EnemyType.LittleShip:
                    scoreValue = LITTLE_SCORE;
                    break;
                case EnemyType.Wizard:
                    scoreValue = WIZZARD_SCORE;
                    break;
                case EnemyType.THOR:
                    scoreValue = THOR_SCORE;
                    break;
            }
        }
    }
}
